<?php namespace App\Http\Controllers\Api\Admin;

use App\Banner;
use App\Classify;
use App\Http\Controllers\Controller;
use App\Content;
use Auth;
use DB;
use Illuminate\Http\Request;

class SystemController extends Controller
{
    public function setBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title   = $request->get('title');
        $linkUrl = $request->get('link_url');
        $sort    = $request->get('sort');
        $imgId   = $request->get('img_id');
        $classify= $request->get('classify');
        $id      = !is_null($request->get('id'))?$request->get('id'):0;

        $thatBanner = Banner::whereId(trim($id))->first();

        if(!$thatBanner)
            $thatBanner = new Banner();

        if(!is_null($title))$thatBanner->title = $title;
        if(!is_null($linkUrl))$thatBanner->link_url = $linkUrl;
        if(!is_null($sort))$thatBanner->sort = $sort;
        if(!is_null($imgId))$thatBanner->img_id = $imgId;
        if(!is_null($classify))$thatBanner->classify_id = $classify;
        $thatBanner->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$thatBanner]);
    }

    public function delBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Banner $thatBanner */
        $thatBanner = Banner::whereId($id)->first();

        if(!$thatBanner)
            return response()->json(["level"=>"error","message"=>"该banner不存在！"]);

        $thatBanner->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatBanner]);
    }

    public function addClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title   = $request->get('title');
        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $newClassify = Classify::whereId($id)->first();
        if($newClassify){
            $newClassify = Classify::whereId($id)->update(['name' => $title]);
        }else{
            $newClassify = Classify::insert(['name' => $title]);
        }

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newClassify]);
    }

    public function delClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatClassify =Classify::whereId($id)->first();

        if(!$thatClassify)
            return response()->json(["level"=>"error","message"=>"该菜单不存在！"]);

        $thatClassify->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatClassify]);
    }

    public function setContent(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title   = $request->get('title');
        $linkUrl   = $request->get('link_url');
        $cId = $request->get('c_id');
        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $newContent = DB::connection('mysql_life')->table('content')->whereId($id)->first();
        if($newContent){
            $newContent = DB::connection('mysql_life')->table('content')->whereId($id)->update(['title' => $title,'c_id' => $cId,'link_url' => $linkUrl]);
        }else{
            $newContent = DB::connection('mysql_life')->table('content')->insert(['title' => $title,'c_id' => $cId,'link_url' => $linkUrl]);
        }

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newContent]);
    }

    public function delContent(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatContent =DB::connection('mysql_life')->table('content')->whereId($id)->first();

        if(!$thatContent)
            return response()->json(["level"=>"error","message"=>"该菜单不存在！"]);

        DB::connection('mysql_life')->table('content')->whereId($id)->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatContent]);
    }

    public function setList(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title     = $request->get('title');
        $linkUrl   = $request->get('link_url');
        $imgId       = $request->get('img_id');
        $id        = !is_null($request->get('id'))?$request->get('id'):0;

        $newContent = DB::connection('mysql_life')->table('list')->whereId($id)->first();
        if($newContent){
            $newContent = DB::connection('mysql_life')->table('list')->whereId($id)->update(['title' => $title,'img_id' => $imgId,'link_url' => $linkUrl]);
        }else{
            $newContent = DB::connection('mysql_life')->table('list')->insert(['title' => $title,'img_id' => $imgId,'link_url' => $linkUrl]);
        }

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newContent]);
    }

    public function delList(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        $thatList =DB::connection('mysql_life')->table('list')->whereId($id)->first();

        if(!$thatList)
            return response()->json(["level"=>"error","message"=>"该菜单不存在！"]);

        DB::connection('mysql_life')->table('list')->whereId($id)->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatList]);
    }

    public function setContents(Request $request){
        if(Auth::guard('admin')->guest()){
            return response()->json(["level"=>"error","message"=>"不允许访问"]);
        }

        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;
        $title    = $request->get("title");
        $content  = $request->get("content");
        $desc     = $request->get("desc");
        $linkUrl  = $request->get("link_url");
        $classify = $request->get("classify");
        $imgId    = $request->get("img_id");
        /** @var Content $thatContents */
        $thatContents = Content::whereId($id)->first();

        if(!$thatContents){
            $thatContents = new Content();
        }

        if(!is_null($title))$thatContents->title = $title;
        if(!is_null($content))$thatContents->content = $content;
        if(!is_null($desc))$thatContents->desc = $desc;
        if(!is_null($linkUrl))$thatContents->link_url = $linkUrl;
        if(!is_null($classify))$thatContents->classify_id = $classify;
        if(!is_null($imgId))$thatContents->img_id = $imgId;

        $thatContents->save();

        return response()->json(["level"=>"success","message"=>"设置完成","data"=>$thatContents]);


    }

    public function delContents(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id       = !is_null($request->get('id')) ? $request->get('id') : 0;

        $newContents = Content::whereId($id)->first();

        if(!$newContents)
            return response()->json(['level'=>'error','message' => '没有该文章！']);

        $newContents->delete();

        return response()->json(["level"=>"success","message"=>"删除完成","data"=>$newContents]);
    }

}